/**
 *
 */
package com.dhcc.android.base.utils.dataparse;

import android.util.Xml;

import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.common.bean.Config;
import com.dhcc.android.base.common.bean.File;
import com.dhcc.android.base.common.bean.Item;
import com.dhcc.android.base.utils.StringUtil;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务包下的config.xml解析文件
 *
 * @author pengling
 * @date 2015-5-24
 */
public class ConfigParse {
    /**
     * 解析XML
     *
     * @return 返回Object
     */
    public Config getXmlObject(InputStream is) {
        Config config = null;
        PullParse pullParse = new PullParse();
        XmlPullParser parser = Xml.newPullParser();
        List<File> fileList = null;
        List<Item> itemList = null;
        List<BasicProperties> actionList = null;
        List<BasicProperties> tableList = null;
        File file = null;
        String dklabelname = "";
        BasicProperties thumbnall = null;
        BasicProperties photo = null;
        BasicProperties add = null;
        BasicProperties maps = null; //新建地块
        BasicProperties nullchecks = null; //新建地块

        BasicProperties mix = null;
        try {
            parser.setInput(is, "UTF-8");
            // 事件类型
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        config = new Config();
                        break;
                    case XmlPullParser.START_TAG:
                        // 获得当前节点元素的名称
                        String nodeName = parser.getName();
                        if ("LEVEL".equalsIgnoreCase(nodeName)) {
                            pullParse.setChildParam(parser, config);
                        } else if ("FILE".equalsIgnoreCase(nodeName)) {
                            file = new File();
                            if (fileList == null) {
                                fileList = new ArrayList<File>();
                            }
                            itemList = new ArrayList<Item>();
                            // 判断标签里是否有属性，如果有，则全部解析出来
                            pullParse.setChildParam(parser, file);
                        } else if ("CHECK".equalsIgnoreCase(nodeName)) {
                            // 判断标签里是否有属性，如果有，则全部解析出来
                            pullParse.setChildParam(parser, file);
                        } else if ("DEFINE".equalsIgnoreCase(nodeName)) {
                            pullParse.setChildParam(parser, file);
                        } else if ("ITEM".equalsIgnoreCase(nodeName)) {
                            Item item = new Item();
                            pullParse.setChildParam(parser, item);
                            if (itemList != null) {
                                itemList.add(item);
                            }
                        } else if ("ADD".equalsIgnoreCase(nodeName)) {//含新增页面
                            add = new BasicProperties();
                            pullParse.setChildParam(parser, add);
                            file.setAdd(add);
                        } else if ("THUMBNAIL".equalsIgnoreCase(nodeName)) {
                            actionList = new ArrayList<BasicProperties>();
                            thumbnall = new BasicProperties();
                            pullParse.setChildParam(parser, thumbnall);
//						file.setThumbnall(thumbnall);
                            dklabelname = thumbnall.getDklabelname();
                        } else if ("PHOTO".equalsIgnoreCase(nodeName)) {
                            photo = new BasicProperties();
                            pullParse.setChildParam(parser, photo);
                            file.setPhoto(photo);
                        } else if ("MAPS".equalsIgnoreCase(nodeName)) {
                            actionList = new ArrayList<BasicProperties>();
                            maps = new BasicProperties();
                            pullParse.setChildParam(parser, maps);
                            dklabelname = maps.getDklabelname();
                        } else if ("NULLCHECKS".equalsIgnoreCase(nodeName)) {
                            tableList = new ArrayList<BasicProperties>();
                            nullchecks = new BasicProperties();
                            pullParse.setChildParam(parser, nullchecks);
                        } else if ("ACTION".equalsIgnoreCase(nodeName)) {
                            BasicProperties action = new BasicProperties();
                            pullParse.setChildParam(parser, action);
                            if (actionList != null) {
                                actionList.add(action);
                            }
                        } else if ("TABLE".equalsIgnoreCase(nodeName)) {
                            BasicProperties table = new BasicProperties();
                            pullParse.setChildParam(parser, table);
                            if (tableList != null) {
                                tableList.add(table);
                            }
                        } else if ("MIX".equalsIgnoreCase(nodeName)) {//含新增页面
                            mix = new BasicProperties();
                            pullParse.setChildParam(parser, mix);
                            file.setMix(mix);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("FILE".equalsIgnoreCase(parser.getName())) {
                            file.setItem(itemList);
                            if (StringUtil.isNotEmpty(dklabelname)) {
                                file.setDklabelname(dklabelname);
                                System.out.println("ConfigParse-----dklabelname:" + dklabelname);
                            }
                            fileList.add(file);
                        }
                        if ("MAPS".equalsIgnoreCase(parser.getName())) {
                            maps.setAction(actionList);
                            file.setMaps(maps);
                        }
                        if ("THUMBNAIL".equalsIgnoreCase(parser.getName())) {
                            thumbnall.setAction(actionList);
                            file.setThumbnall(thumbnall);
                        }
                        if ("NULLCHECKS".equalsIgnoreCase(parser.getName())) {
                            nullchecks.setTable(tableList);
                            file.setNullchecks(nullchecks);
                        }
                        if ("LEVEL".equalsIgnoreCase(parser.getName())) {
                            config.setFile(fileList);
                        }
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return config;
    }


    /**
     * 解析XML
     *
     * @return 返回Object
     */
    public Config getXmlObject(InputStream is, String period) {
        Config config = null;
        PullParse pullParse = new PullParse();
        XmlPullParser parser = Xml.newPullParser();
        List<File> fileList = null;
        List<Item> itemList = null;
        List<BasicProperties> actionList = null;
        List<BasicProperties> tableList = null;
        File file = null;
        String dklabelname = "";
        BasicProperties thumbnall = null;
        BasicProperties photo = null;
        BasicProperties add = null;
        BasicProperties maps = null; //新建地块
        BasicProperties nullchecks = null; //新建地块

        BasicProperties mix = null;
        try {
            parser.setInput(is, "UTF-8");
            // 事件类型
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        config = new Config();
                        break;
                    case XmlPullParser.START_TAG:
                        // 获得当前节点元素的名称
                        String nodeName = parser.getName();
                        if ("LEVEL".equalsIgnoreCase(nodeName)) {
                            pullParse.setChildParam(parser, config);
                        } else if ("FILE".equalsIgnoreCase(nodeName)) {
                            file = new File();
                            if (fileList == null) {
                                fileList = new ArrayList<File>();
                            }
                            itemList = new ArrayList<Item>();
                            // 判断标签里是否有属性，如果有，则全部解析出来
                            pullParse.setChildParam(parser, file);
                        } else if ("CHECK".equalsIgnoreCase(nodeName)) {
                            // 判断标签里是否有属性，如果有，则全部解析出来
                            pullParse.setChildParam(parser, file);
                        } else if ("DEFINE".equalsIgnoreCase(nodeName)) {
                            pullParse.setChildParam(parser, file);
                        } else if ("ITEM".equalsIgnoreCase(nodeName)) {
                            Item item = new Item();
                            pullParse.setChildParam(parser, item);
                            if (itemList != null) {
                                itemList.add(item);
                            }
                        } else if ("ADD".equalsIgnoreCase(nodeName)) {//含新增页面
                            add = new BasicProperties();
                            pullParse.setChildParam(parser, add);
                            file.setAdd(add);
                        } else if ("THUMBNAIL".equalsIgnoreCase(nodeName)) {
                            actionList = new ArrayList<BasicProperties>();
                            thumbnall = new BasicProperties();
                            pullParse.setChildParam(parser, thumbnall);
//						file.setThumbnall(thumbnall);
                            dklabelname = thumbnall.getDklabelname();
                        } else if ("PHOTO".equalsIgnoreCase(nodeName)) {
                            photo = new BasicProperties();
                            pullParse.setChildParam(parser, photo);
                            file.setPhoto(photo);
                        } else if ("MAPS".equalsIgnoreCase(nodeName)) {
                            actionList = new ArrayList<BasicProperties>();
                            maps = new BasicProperties();
                            pullParse.setChildParam(parser, maps);
                            dklabelname = maps.getDklabelname();
                        } else if ("NULLCHECKS".equalsIgnoreCase(nodeName)) {
                            tableList = new ArrayList<BasicProperties>();
                            nullchecks = new BasicProperties();
                            pullParse.setChildParam(parser, nullchecks);
                        } else if ("ACTION".equalsIgnoreCase(nodeName)) {
                            BasicProperties action = new BasicProperties();
                            pullParse.setChildParam(parser, action);
                            if (actionList != null) {
                                actionList.add(action);
                            }
                        } else if ("TABLE".equalsIgnoreCase(nodeName)) {
                            BasicProperties table = new BasicProperties();
                            pullParse.setChildParam(parser, table);
                            if (tableList != null) {
                                tableList.add(table);
                            }
                        } else if ("MIX".equalsIgnoreCase(nodeName)) {//含新增页面
                            mix = new BasicProperties();
                            pullParse.setChildParam(parser, mix);
                            file.setMix(mix);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("FILE".equalsIgnoreCase(parser.getName())) {
                            if ("TRUE".equals(file.getConfirm())) {//页面是否是固定的
                                file.setItem(itemList);
                                if (StringUtil.isNotEmpty(dklabelname)) {
                                    file.setDklabelname(dklabelname);
                                    System.out.println("ConfigParse-----dklabelname:" + dklabelname);
                                }
                                fileList.add(file);

                            } else {//不是固定的
                                //如果是前面选择的调查期对应的页面，就加进去
                                if (file.getPeriod() != null && period.equals(file.getPeriod())) {
                                    file.setItem(itemList);
                                    if (StringUtil.isNotEmpty(dklabelname)) {
                                        file.setDklabelname(dklabelname);
                                        System.out.println("ConfigParse-----dklabelname:" + dklabelname);
                                    }
                                    fileList.add(file);
                                }
                            }

                        }
                        if ("MAPS".equalsIgnoreCase(parser.getName())) {
                            maps.setAction(actionList);
                            file.setMaps(maps);
                        }
                        if ("THUMBNAIL".equalsIgnoreCase(parser.getName())) {
                            thumbnall.setAction(actionList);
                            file.setThumbnall(thumbnall);
                        }
                        if ("NULLCHECKS".equalsIgnoreCase(parser.getName())) {
                            nullchecks.setTable(tableList);
                            file.setNullchecks(nullchecks);
                        }
                        if ("LEVEL".equalsIgnoreCase(parser.getName())) {
                            config.setFile(fileList);
                        }
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return config;
    }

}
